package com.monitor.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Download {
	public static String downloadFile(String path,String fileName, HttpServletRequest request,
            HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
        try {
            /*String path = Thread.currentThread().getContextClassLoader()
                    .getResource("").getPath()
                    + "download";//这个download目录为啥建立在classes下的
            */
            InputStream inputStream = new FileInputStream(new File(path+ File.separator + fileName));

            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }

             // 这里主要关闭。
            os.close();

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
            //  返回值要注意，要不然就出现下面这句错误！
            //java+getOutputStream() has already been called for this response
        return null;
	}
	
	
	public static String downloadFileName(String path,String fileName, HttpServletRequest request,
            HttpServletResponse response,String name) throws Exception {
		
		File file=new File(path+ File.separator + fileName); //指定文件名及路径
		  String filename=file.getAbsolutePath();
		  if(filename.indexOf(".")>=0)
		  {
		   filename = filename.substring(0,filename.lastIndexOf("."));
		  }
		//file.renameTo(new File(path+ File.separator + name)); //改名
		  UploadUtil.copyFile(path+fileName, path+ File.separator + name);
		
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        //response.setHeader("Content-Disposition", "attachment;fileName="+ name);
        
        response.setHeader("Content-Disposition", "attachment;filename="  
                + new String(name.getBytes(),"iso-8859-1"));
        try {
            /*String path = Thread.currentThread().getContextClassLoader()
                    .getResource("").getPath()
                    + "download";//这个download目录为啥建立在classes下的
            */
            InputStream inputStream = new FileInputStream(new File(path+ File.separator + name));

            OutputStream os = response.getOutputStream();	
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }

             // 这里主要关闭。
            os.close();

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
            //  返回值要注意，要不然就出现下面这句错误！
            //java+getOutputStream() has already been called for this response
        UploadUtil.deleteFile( path+ File.separator + name, request);
       
        return null;
	}
}
